﻿using FlowCanvas;
using FlowCanvas.Nodes;
using ParadoxNotion.Design;
using System.Collections;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;

namespace Yoozoo.Gameplay.Liberty.Nodes
{
    [Category("Ggame")]
    public class NpcSetPosAndRotation : LatentActionNodeBase
    {

        protected override void OnRegisterDerivedPorts(FlowNode node)
        {
            var p1 = node.AddValueInput<int>(parameters[0].Name);
            var p2 = node.AddValueInput<Vector3>(parameters[1].Name);
            var p3 = node.AddValueInput<Vector3>(parameters[2].Name);

            node.AddFlowInput("In", (f) => { 
                Begin(Invoke(p1.value, p2.value, p3.value), f); 
            });
        }

        public IEnumerator Invoke(int npcId, Vector3 position, Vector3 rotation)
        {
            if (LibertyAIUtils.PLAYER_AI_ID == npcId && LibertyAIUtils.PlayerController)
            {
                // 需要切换到输入模式，否则会被寻路Seeker控制
                LibertyAIUtils.PlayerController.Switch2InputMode();
                yield return null;
                LibertyAIUtils.PlayerController.PlayerTransmission(position,rotation,false);
            }
            else
            {
                var npcObj = BlueprintUtils.GetNpc(npcId);
                var rigidBody = BlueprintUtils.GetPlayerNpcRigidBody();
                rigidBody.velocity = Vector3.zero;
                npcObj.transform.position = position;
                npcObj.transform.rotation = Quaternion.Euler(rotation);

                yield return new UnityEngine.WaitUntil(() => { return true; });
            }
        }
    }
}
